/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

/**
 *
 * @author Alberto
 */
public class SingletonDao {

    private static SingletonDao INSTANCE = null;
    private static EntityManager em;
    private static EntityManagerFactory emf;
    private static EntityTransaction tx;

    // Private constructor suppresses
    private SingletonDao() {
        emf = Persistence.createEntityManagerFactory("domecoPU");
        em = emf.createEntityManager();
        tx = em.getTransaction();
    }

    // creador sincronizado para protegerse de posibles problemas  multi-hilo
    // otra prueba para evitar instanciación múltiple
    private synchronized static void createInstance() {
        if (INSTANCE == null) {
            INSTANCE = new SingletonDao();
        }
    }

    public static SingletonDao getInstance() {
        if (INSTANCE == null) {
            createInstance();
        }
        return INSTANCE;
    }

    public EntityManager getEntityManager(){
        return em;
    }

    public static EntityTransaction getTx() {
        return tx;
    }
}
